package com.kanshu.ksgb.fastread.doudou.common.business.manager;


import android.util.Log;

import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.UserUtils;
import com.tencent.mmkv.MMKV;

/**
 * mmkv管理器
 * 作者    wcy
 * 时间    2018/11/16 10:04
 * 文件    doudou
 * 描述
 */
public class MMKVUserManager {

    private String KEY_USER_MMKV= "config_";

    private volatile static MMKVUserManager sManager;
    // mmkv实例
    private MMKV mMMKV;
    private MMKVUserManager() {
        mMMKV = MMKV.mmkvWithID(KEY_USER_MMKV + UserUtils.getUserId());
    }

    public static MMKVUserManager getInstance() {
        if (sManager == null) {
            synchronized (MMKVUserManager.class) {
                if (sManager == null) {
                    sManager = new MMKVUserManager();
                }
            }
        }
        return sManager;
    }

    public static void reSetMMKV() {
        sManager = null;
        synchronized (MMKVUserManager.class) {
            if (sManager == null) {
                sManager = new MMKVUserManager();
            }
        }
    }

    public MMKV getMMKV(){
        return mMMKV;
    }

    /**
     * 保存登录状态
     */
    public void setLoginState(int loginState){
        mMMKV.putInt("login_state", loginState);
    }

    /**
     * 获取登录状态
     */
    public int getLoginState(){
        return mMMKV.getInt("login_state", 0);
    }

    /**
     * 获取是否登录
     * @return ture为登录状态
     */
    public boolean isUserLogin() {
        return getLoginState() == Constants.LoginState.TYPE_HAS_LOGIN;
    }

    /**
     * 保存当前用户token
     */
    public void setLoginToken(String DeviceUserId){
        mMMKV.putString("login_token", DeviceUserId);
    }

    /**
     * 获取当前用户token
     */
    public String getLoginToken(){
        return mMMKV.getString("login_token", "");
    }

    /**
     * 保存当前登录用户性别
     */
    public void setLoginSex(String DeviceUserId){
        mMMKV.putString("login_sex", DeviceUserId);
    }

    /**
     * 获取当前登录用户性别
     */
    public String getLoginSex(){
        return mMMKV.getString("login_sex", "1");
    }

    /**
     * 保存当前登录用户金豆数量
     */
    public void setLoginBeans(String loginBeans){
        mMMKV.putString("login_beans", loginBeans);
    }

    /**
     * 获取当前登录用户金豆数量
     */
    public String getLoginBeans(){
        return mMMKV.getString("login_beans", "0");
    }

    /**
     * 保存书籍下载失败原因
     */
    public void saveDownLoadFailBecause(String bookId, String because){
        mMMKV.putString("download_fail_because_" + bookId, because);
    }

    /**
     * 保存书籍下载失败原因
     */
    public String getDownLoadFailBecause(String bookId){    // 默认原因 网络中断，下载失败。
        return mMMKV.getString("download_fail_because_" + bookId, "网络中断，下载失败");
    }

    /**
     * 保存下载失败 分组Id
     */
    public void saveDownLoadFailGroupId(String bookId, String groupId, String bookTitle){
        String failGroup = getDownLoadFailGroupId(bookId);
        String failInfo = "{\"id\": \"" + groupId + "\",\"book_title\": \"" + bookTitle + "\"}";
        // 防止重复保存失败信息
        if(!failGroup.contains(failInfo)){
            failGroup = failGroup.substring(0, failGroup.length() - 1);
            failGroup += (failGroup.length() < Constants.DOWNLOAD_FAILINFO_DEFAULT_LENGTH ?  "" : ",") + failInfo;
            mMMKV.putString("download_fail_group_" + bookId, failGroup + "]");
        }
    }

    /**
     * 下载成功 删除保存的组任务信息
     */
    public void downLoadSuccDeleteGroupId(String bookId, String groupId, String bookTitle){
        String failGroup = getDownLoadFailGroupId(bookId);
        String failInfo = "{\"id\": \"" + groupId + "\",\"book_title\": \"" + bookTitle + "\"}";
        // 有失败的组任务信息  才进行删除
        if(failGroup.contains(failInfo)){
            if(failGroup.length() < failInfo.length() + Constants.DOWNLOAD_FAILINFO_DEFAULT_LENGTH) { // 只有一条记录
                failGroup = failGroup.replace(failInfo, "");
            } else if(failGroup.contains(failInfo + ",") || failGroup.startsWith("[" + failInfo)){ // 有多条记录 处于中间或处于第一条
                failGroup = failGroup.replace(failInfo + ",", "");
            }else if(failGroup.startsWith("[" + failInfo)){ // 有多条记录 且处于最后一条
                failGroup = failGroup.replace("," + failInfo, "");
            }
            Log.d("downLoad  ", failGroup);
            mMMKV.putString("download_fail_group_" + bookId, failGroup);
        }
    }

    /**
     * 下载成功 删除保存的章节信息
     */
    public void downLoadSuccDeleteChapter(String bookId, String groupId, String bookTitle, String contentId, int order){
        String failChapter = getDownLoadFailChapter(bookId, groupId);
        String failInfo = "{\"book_id\": \"" + bookId + "\",\"content_id\": \"" + contentId + "\",\"order\": " + order + "}";
        // 有失败的章节信息  才进行删除
        if(failChapter.contains(failInfo)){
            if(failChapter.length() < failInfo.length() + Constants.DOWNLOAD_FAILINFO_DEFAULT_LENGTH) { // 只有一条记录 删除组ID数据
                failChapter = failChapter.replace(failInfo, "");
                downLoadSuccDeleteGroupId(bookId, groupId, bookTitle);
            } else if(failChapter.contains(failInfo + ",") || failChapter.startsWith("[" + failInfo)){ // 有多条记录 处于中间或处于第一条
                failChapter = failChapter.replace(failInfo + ",", "");
            }else if(failChapter.startsWith("[" + failInfo)){ // 有多条记录 且处于最后一条
                failChapter = failChapter.replace("," + failInfo, "");
            }
            Log.d("downLoad  ", failChapter);
            mMMKV.putString("download_fail_" + bookId + "_" + groupId, failChapter);
        }
    }

    /**
     * 重置 下载失败分组Id
     */
    public void reSetDownLoadFailGroupId(String bookId){
        mMMKV.putString("download_fail_group_" + bookId, "[]");
    }

    /**
     * 获取下载失败分组信息
     */
    public String getDownLoadFailGroupId(String bookId){
        return mMMKV.getString("download_fail_group_" + bookId, "[]");
    }

    /**
     * 分组保存下载章节失败信息
     */
    public void saveDownLoadFailChapter(String bookId, String groupId, String contentId, int order){
        String failChapter = getDownLoadFailChapter(bookId, groupId);
        String failInfo = "{\"book_id\": \"" + bookId + "\",\"content_id\": \"" + contentId + "\",\"order\": " + order + "}";
        // 防止重复保存失败信息
        if(!failChapter.contains(failInfo)){
            failChapter = failChapter.substring(0, failChapter.length() - 1);
            failChapter += (failChapter.length() < Constants.DOWNLOAD_FAILINFO_DEFAULT_LENGTH ?  "" : ",") + failInfo;
            mMMKV.putString("download_fail_" + bookId + "_" + groupId, failChapter + "]");
        }
    }

    /**
     * 重置 下载失败章节信息
     */
    public void reSetDownLoadFailChapter(String bookId, String groupId){
        mMMKV.putString("download_fail_" + bookId + "_" + groupId, "[]");
    }

    /**
     * 获取当前登录用户下载失败章节信息
     */
    public String getDownLoadFailChapter(String bookId, String groupId){
        return mMMKV.getString("download_fail_" + bookId + "_" + groupId, "[]");
    }

    /**
     * 检查章节是否下载失败
     */
    public boolean checkChapterIsFail(String bookId, String groupId, String contentId, int order){
        String failChapter = getDownLoadFailChapter(bookId, groupId);
        String failInfo = "{\"book_id\": \"" + bookId + "\",\"content_id\": \"" + contentId + "\",\"order\": " + order + "}";
        // 防止重复保存失败信息
        return failChapter.contains(failInfo);
    }

    /**
     * 保存书籍下载章节数
     */
    public void saveBookDownloadChapterCount(String bookId, int chapterCount){
        mMMKV.putInt("download_chapter_count_" + bookId, chapterCount);
    }

    /**
     * 获取书籍下载章节数
     */
    public int getBookDownloadChapterCount(String bookId){
        return mMMKV.getInt("download_chapter_count_" + bookId, 0);
    }




}
